import prompt from '@system.prompt';
import featureAbility from '@ohos.ability.featureAbility';
import dataStorage from '@ohos.data.storage';
import router from '@system.router';
import http from '@ohos.net.http';

const TAG = '[index]';
var context;

export default {
    data: {
        login:'login',
        password_flag: "password",
        password_img01: "/common/images/hide.png",
        temPasswordValue: "",
        phonenumber:'',
        password:'',
        repassword:'',
        sex:'',
        username:'',
        headshot:'',
        context:null,
        path:'',
    },
    async onInit() {
        console.info(TAG + 'onInit!!!!!!!!!!!!!!!!!!!');
        context = this;
        this.title = this.$t('strings.title');
        this.context = featureAbility.getContext();
        this.path = await this.context.getFilesDir()
    },
    hide() {
        if (context.password_flag == "password") {
            context.password_flag = "text";
            context.password_img01 = "/common/images/show.png";
        } else if (context.password_flag == "text") {
            context.password_flag = "password";
            context.password_img01 = "/common/images/hide.png";
        }
    },
    async clickfun(params) {
        this.context = featureAbility.getContext();
        this.path = await this.context.getFilesDir()
        let storage = dataStorage.getStorageSync(this.path + '/mystore')
        storage.putSync('password', this.temPasswordValue)
        storage.flushSync()
    },
    Phonenumber(pramas){
        this.phonenumber = pramas.text
    },
    Password(params) {
        this.password = params.text;
    },
    rePassword(params) {
        this.repassword = params.text;
    },
    homePage(){
        router.back();
    },
    Login(){
        let httpRequest= http.createHttp();
        httpRequest.request("http://101.35.82.76:9002/user/login?"+"phonenumber="+this.phonenumber+"&password="+this.password,
            {
                method: 'GET',
//                header: {
//                    'Content-Type': 'text/html;charset=UTF-8'
//                },
                readTimeout: 60000,
                connectTimeout: 60000
            },(err, data) => {
                if (!err) {
                    console.info('Result:' + data.result);
                    console.info('code:' + data.responseCode);
                    console.info('header:' + data.header);
//                    prompt.showToast({
//                        message: data.result
//                    })
                    var res = JSON.stringify(JSON.parse(data.result).data)
                    var userInfo = JSON.stringify(JSON.parse(res).userInfo)
                    userInfo = JSON.parse(userInfo)

                    prompt.showToast({
                        message: '登录成功'
                    })

                    let storage = dataStorage.getStorageSync(this.path + '/mystore')
                    storage.putSync('userid', userInfo.userid)
                    storage.putSync('username', userInfo.username)
                    storage.putSync('password', userInfo.password)
                    storage.putSync('email', userInfo.email)
                    storage.putSync('headshot', userInfo.headshot)
                    storage.putSync('sex', userInfo.sex)
                    storage.putSync('phonenumber', userInfo.phonenumber)
                    storage.flushSync()
                    router.back()
                } else {
                    console.info('error:' + err.data);
                    prompt.showToast({
                        message: 'Send Error'+ JSON.stringify(err)
                    })
                }
            });
    },
    toRegister(){
        this.login = 'register';
    },
    Register(){
        if(this.password !== this.repassword){
            prompt.showToast({
                message:'密码不一致'
            })
            return;
        }
        var that = this

        let httpRequest= http.createHttp();
        httpRequest.request("http://101.35.82.76:9002/user/register?phonenumber="+this.phonenumber+
        '&password='+this.password,
            {
                method: 'GET',
                readTimeout: 60000,
                connectTimeout: 60000
            },(err, data) => {
                if (!err) {
                    console.info('Result:' + data.result);
                    console.info('code:' + data.responseCode);
                    console.info('header:' + data.header);

                    prompt.showToast({
                        message: '注册成功'
                    })
                    this.Login()
                    router.back()
                } else {
                    console.info('error:' + err.data);
                    prompt.showToast({
                        message: 'Send Error'+ JSON.stringify(err)
                    })
                }
            });
    },
}
